package com.wangwei.authoritySystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangwei.authoritySystem.common.utils.DepartmentTree;
import com.wangwei.authoritySystem.common.utils.MenuTree;
import com.wangwei.authoritySystem.pojo.Department;
import com.wangwei.authoritySystem.pojo.Permission;
import com.wangwei.authoritySystem.service.PermissionService;
import com.wangwei.authoritySystem.mapper.PermissionMapper;
import com.wangwei.authoritySystem.vo.PermissionQueryVo;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 *
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{

    //根据用户ID查询用户权限列表
    @Override
    public List<Permission> findPermissionListByUserId(Long userId) {
        return baseMapper.findPermissionListByUserId(userId);
    }

    @Override
    public List<Permission> findPermissionList(PermissionQueryVo permissionQueryVo) {
        //先查询出部列表
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
        queryWrapper.orderByAsc("order_num");
        List<Permission> permissionList = baseMapper.selectList(queryWrapper);
        //生成树
        List<Permission> PermissionTree = MenuTree.createMenuTree(permissionList,0L);
        return PermissionTree;
    }

    @Override
    public List<Permission> findParentPermissionList() {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
        //只查询目录(0)和菜单(1)的数据
        queryWrapper.in("type", Arrays.asList(0,1));
        queryWrapper.orderByAsc("order_num");
        List<Permission> permissionList = baseMapper.selectList(queryWrapper);
        //生成树
        List<Permission> PermissionTree = MenuTree.createMenuTree(permissionList,0L);
        return PermissionTree;
    }

    @Override
    public boolean hasChildren(Long id) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
        queryWrapper.eq("parent_id",id);
        if(baseMapper.selectCount(queryWrapper)>0){
            return true;
        }
        return false;
    }
}




